$(document).ready(function () {
    $.getJSON('/parking/totalpage', function (data) {
        $("#total").html(data);
        if( data=="0"){
            $("#current").html(0);
        }else{
            $("#current").html(1);
        }
    });
    if($("#carId").val()=="") {
        list(1);

    }
    $("#newMemberName").blur(function(){
        if($("#newMemberName").val()!=""){
            $("#p1").html("")
        }else{
            $("#p1").html("姓名不能为空")
        }
    })
    $("#newLicensePlate").blur(function(){
        var flag=/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/
        if(flag.test($(this).val())){
            $("#p2").html("")
        }else if($(this).val()==""){
            $("#p2").html("车牌号不能为空")
        }else{
            $("#p2").html("请输入正确车牌号形式")
        }
    })
    $("#newMemberPhone").blur(function(){
        var flag=/^1[34578]\d{9}$/
        if(flag.test($(this).val())){
            $("#p3").html("")
        }else if($(this).val()==""){
            $("#p3").html("手机号不能为空")
        }else{
            $("#p3").html("请输入正确手机号")
        }
    })
    $("#newMemberBalance").blur(function(){
        if($("#newMemberBalance").val()!=""){
            $("#p4").html("")
        }else{
            $("#p4").html("充值金额不能为空")
        }
    })
    $("#memberPhone").blur(function(){
        var flag=/^1[34578]\d{9}$/
        if(flag.test($(this).val())){
            $("#p5").html("")
        }else if($(this).val()==""){
            $("#p5").html("手机号不能为空")
        }else{
            $("#p5").html("请输入正确手机号")
        }
    })
    $("#recharge").blur(function(){
        if($(this).val()!=""){
            $("#p6").html("")
        }else{
            $("#p6").html("充值金额不能为空")
        }
    })
});

//分页查所有
function list(current){
    $.getJSON('/parking/totalpage', function (data) {
        $("#total").html(data);
    });
    $.getJSON('/parking/userlist', {"current": current}, function (data) {
        var options;
        var x;
        if (data != null) {
            $("tbody").empty();
            for (var i = 0; i < data.length; i++) {
                if(data[i].userStatus==1){
                    x="是"
                }else{
                    x="否"
                }
                options += "<tr><td  style='display:table-cell; vertical-align:middle'>" + data[i].userId + "</td>\n" +
                    "<td  style='display:table-cell; vertical-align:middle'>" + data[i].userName + "</td>\n" +
                    "<td  style='display:table-cell; vertical-align:middle'>" + data[i].userCarId + "</td>\n" +
                    "<td  style='display:table-cell; vertical-align:middle'>" + data[i].userPhone + "</td>\n" +
                    "<td  style='display:table-cell; vertical-align:middle'>" + data[i].userBalance + "</td>\n" +
                    "<td  style='display:table-cell; vertical-align:middle'>" + x + "</td>\n" +
                    "<td> <div class='btn-group'>\n" +
                    "<a class='btn btn-xs btn-default' data-target='#editModal' title='编辑' data-toggle='modal' onclick='give(" + data[i].userId + ",\"" + data[i].userName + "\",\"" + data[i].userCarId + "\"," + data[i].userPhone + "," + data[i].userBalance + ","+data[i].userAmount+","+data[i].userStatus+")'><i class='mdi mdi-pencil'></i></a>\n" +
                    "<a class='btn btn-xs btn-default del'  title='删除' data-toggle='tooltip' onclick='deleteUser("+data[i].userId +")' userid="+data.userId+" username="+data.userName+"><i class='mdi mdi-window-close'></i></a>\n" +
                    "<button type='button' class='btn btn-xs btn-primary' data-toggle='modal' data-target='#exampleModal' onclick='giveid("+data[i].userId +"," + data[i].userBalance + ",\"" + data[i].userCarId + "\","+data[i].userAmount+","+data[i].userStatus+",\""+data[i].userName+"\")'>充值</button> </div> </td><tr>";
            }
            $("tbody").append(options);
        }
    });
}

//首页方法
function first(){
    $("#current").html(1);
    if($("#carId").val()=="") {
        list(1);
    }else{
        search(1);
    }
}

//尾页方法
function last(){
    if($("#carId").val()=="") {
        list($("#total").html());
    }else{
        search($("#total").html());
    }
    $("#current").html($("#total").html());
}

//下一页方法
function next(){
    if(Number($("#current").html())<Number($("#total").html())) {
       var x=Number($("#current").html()) + 1;
        if($("#carId").val()=="") {
            list(x);
        }else{
            search(x);
        }
        $("#current").html(x);
    }
}

//上一页方法
function previous(){
    if(Number($("#current").html())>1){
        var x=$("#current").html()-1;
        if($("#carId").val()=="") {
            list(x);
        }else{
            search(x);
        }
        $("#current").html(x);
    }
}

//模糊查询
function search(current) {
    if($("#carId").val()!=""){
        $.getJSON('/parking/searchuser', {"carId": $("#carId").val(),"current": current},
            function (data) {
                $("tbody").empty();
                for (var i=0;i<data.length;i++) {
                    $("tbody").append(insertTableRow(data[i]));
                }
            }
        );
        $.getJSON('/parking/totalpage2',{"carId": $("#carId").val()}, function (data) {
            $("#total").html(data);
            if(data=="0"){
                $("#current").html(0);
            }
        });
    }else{
        $.getJSON('/parking/totalpage', function (data) {
            $("#total").html(data);
            if(data=="0"){
                $("#current").html(0);
            }
        });

        list(1);

    }
    $("#current").html(1);

}

function insertTableRow(data) {
    var x;
    if(data.userStatus==1){
        x="是"
    }else{
        x="否"
    }
    return "<tr><td  style='display:table-cell; vertical-align:middle'>" + data.userId + "</td>\n" +
        "<td  style='display:table-cell; vertical-align:middle'>" + data.userName + "</td>\n" +
        "<td  style='display:table-cell; vertical-align:middle'>" + data.userCarId + "</td>\n" +
        "<td  style='display:table-cell; vertical-align:middle'>" + data.userPhone + "</td>\n" +
        "<td  style='display:table-cell; vertical-align:middle'>" + data.userBalance + "</td>\n" +
        "<td  style='display:table-cell; vertical-align:middle'>" + x + "</td>\n" +
        "<td> <div class='btn-group'>\n" +
        "<a class='btn btn-xs btn-default' data-target='#editModal' title='编辑' data-toggle='modal' onclick='give(" + data.userId + ",\"" + data.userName + "\",\"" + data.userCarId + "\"," + data.userPhone + "," + data.userBalance + ","+data.userAmount+","+data.userStatus+")'><i class='mdi mdi-pencil'></i></a>\n" +
        "<a class='btn btn-xs btn-default del'  title='删除' data-toggle='tooltip' onclick='deleteUser("+data.userId +")' userid="+data.userId+" username="+data.userName+"><i class='mdi mdi-window-close'></i></a>\n" +
        "<button type='button' class='btn btn-xs btn-primary' data-toggle='modal' data-target='#exampleModal' onclick='giveid("+data.userId +"," + data.userBalance + ",\"" + data.userCarId + "\","+data.userAmount+","+data.userStatus+",\""+data.userName+"\")'>充值</button> </div> </td><tr>";
}

//删除用户方法
function deleteUser(userid) {
    var x;
    $.ajax({
        type: "GET",
        url: "/parking/userDel",
        data: {userId: userid},
        dataType: "json",
        success: function (data) {
            if (data.delResult == "true") {//删除成功：移除删除行
                if($("#carId").val()!=""){
                    x=$("#current").html();
                    search($("#current").html());
                    $("#current").html(x);
                }else{
                    list($("#current").html());
                }
            } else if (data.delResult == "false") {//删除失败
                alert("对不起，删除用户失败");
            } else if (data.delResult == "notexist") {
                alert("对不起，用户不存在");
            }
        },
        error: function (data) {
            alert("对不起，删除失败");

        }
    });
}

//新增用户方法
function newCreate(){

    if($("#p1").html()=="" && $("#p2").html()=="" && $("#p3").html()=="" && $("#p4").html()=="" &&
        $("#newMemberName").val()!="" && $("#newLicensePlate").val()!="" &&  $("#newMemberPhone").val()!=""
        && $("#newMemberBalance").val()!=""){
        $.ajax({
            type: "GET",
            url: "/parking/userAdd",
            data: {userName: $("#newMemberName").val(),userCarId: $("#newLicensePlate").val(),userPhone: $("#newMemberPhone").val(),userBalance: $("#newMemberBalance").val()},
            dataType: "json",
            success: function (data) {
                var x;
                if (data.addResult == "true") {
                    alert("新增成功")
                    $("#newMemberName").val("");
                    $("#newLicensePlate").val("");
                    $("#newMemberPhone").val("");
                    $("#newMemberBalance").val("");
                    $("#newCreateModal").modal('hide');
                    $.getJSON('/parking/totalpage', function (data) {
                        $("#total").html(data);
                    });
                    if($("#carId").val()!=""){
                        x=$("#current").html();
                        search($("#current").html());
                        $("#current").html(x);
                    }else{
                        list($("#current").html());
                    }
                }
            },
            error: function (data) {
                alert("对不起，新增失败");

            }
        });
    }

}

//充值方法
    function giveid(userid,userbalance,usercarid,useramount,userstatus,username){
        $("#uid").val(userid)
        $("#ubalance").val(userbalance)
        $("#uamount").val(useramount)
        $("#ustatus").val(userstatus)
        $("#uname").val(username)
        $("#lp").val(usercarid)
    }
    function recharge(){
        if($("#recharge").val()!=""){
            $.ajax({
                type: "GET",
                url: "/parking/userUpd",
                data: {userId: $("#uid").val(),userBalance: Number($("#recharge").val())+Number($("#ubalance").val()),userAmount: $("#uamount").val(),userStatus: $("#ustatus").val()},
                dataType: "json",
                success: function (data) {
                    var x;
                    if (data.updResult == "true") {
                        alert("充值成功")
                        $("#recharge").val("")
                        $("#exampleModal").modal('hide');
                        if($("#carId").val()!=""){
                            x=$("#current").html();
                            search($("#current").html());
                            $("#current").html(x);
                        }else{
                            list($("#current").html());
                        }
                    }
                },
                error: function (data) {
                    alert("对不起，充值失败");

                }
            });

            $.ajax({
                type: "GET",
                url: "/parking/rechargeAdd",
                data: {rechargeUserId: $("#uid").val(),rechargeUserName: $("#uname").val(),rechargeCarId: $("#lp").val(),rechargeAmount: $("#recharge").val()},
                dataType: "json",
                success: function (data) {
                }
            });
        }

    }

    //修改方法
function give(userid,username,usercarid,userphone,userbalance,useramount,userstatus){
    $("#memberId").val(userid)
    $("#memberName").val(username)
    $("#licensePlate").val(usercarid)
    $("#memberPhone").val(userphone)
    $("#memberBalance").val(userbalance)
    $("#uamount2").val(useramount)
    $("#ustatus2").val(userstatus)
}

function edit(){
    if( $("#memberPhone").val()!=""){
        $.ajax({
            type: "GET",
            url: "/parking/userUpd",
            data: {userId: $("#memberId").val(),userPhone:  $("#memberPhone").val(),userBalance: Number($("#memberBalance").val()),userAmount: $("#uamount2").val(),userStatus: $("#ustatus2").val()},
            dataType: "json",
            success: function (data) {
                var x;
                if (data.updResult == "true") {
                    alert("修改成功")
                    $("#editModal").modal('hide');
                    if($("#carId").val()!=""){
                        x=$("#current").html();
                        search($("#current").html());
                        $("#current").html(x);
                    }else{
                        list($("#current").html());
                    }
                }
            },
            error: function (data) {
                alert("对不起，修改失败");

            }
        });
    }

}




